// packageI/vending_machine/vending_machine.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    SearchValue: "",
    Height: 0,
    LastHeight: 0,
    language: '',

    list: [],
    last_page: 0,
    current_page: 0,


    banner:"",
    mini_url:"",
    machineId:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          console.log(chil_arr);
          if (chil_arr[0] == 'machine_id') {
            this.setData({
              machineId: chil_arr[1]
            });
          } else if (chil_arr[0] == 'mid') {
            app._setMid(chil_arr[1]);
          }
        }
      }
    }else if(options.id || options.machine_id){
      this.setData({machineId:options.id || options.machine_id})
    }
    this.getBanner()
    this.getData();
    this.imgHeight();
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
    
  },
  link(){
    wx.navigateTo({
      url: this.data.mini_url,
    })
  },
  getBanner(){
    app._getNetWork({
      url: app.getNetAddresss('plugin.automat.frontend.index.index'),
      data: { automat_machine_id: this.data.machineId },
      success: ({
        data: {
          result,
          msg,
          data
        }
      }) => {
        if (result) {
          this.setData({
            mini_url: data.mini_route,
            banner: data.banner,
            machineId:data.id
          })
          wx.setNavigationBarTitle({
            title: data.name,
          })
        } else {
          app.tips(msg);
        }
      }
    })
  },
  toSearch(){
    wx.navigateTo({
      url: '/packageB/member/category/search_v2/search_v2?machineId='+this.data.machineId
    })
  },
  link_order(){
    wx.navigateTo({
      url: '/packageA/member/myOrder_v2/myOrder_v2?status=0',
    })
  },
  buy(e){
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id='+e.currentTarget.dataset.id+'&machineId='+this.data.machineId,
    })
  },
  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    if (e && e.detail) {
      var imgh = e.detail.height; //图片高度
      var imgw = e.detail.width; //图片宽度
      var swiperH = (winWid * imgh) / imgw; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
      if (parseInt(swiperH) > this.data.LastHeight) {
        this.setData({
          Height: swiperH, //设置高度
        });
        this.data.LastHeight = parseInt(swiperH);
      }
    }

  },
  getData(pages){
    let page = pages ? pages :1;
    let json = {
      automat_machine_id: this.data.machineId,
      "search[automat_machine_id]": this.data.machineId,
      page
    }
    app._getNetWork({
      url: app.getNetAddresss('plugin.automat.frontend.goods.searchGoods'),
      data: json,
      success: ({
        data: {
          result,
          msg,
          data
        }
      }) => {
        if (result) {
          this.setData({
            last_page: data.last_page,
            current_page: data.current_page
          })
          if (page == 1) {
            this.setData({
              list: data.data
            })
          } else {
            this.setData({
              list: this.data.list.concat(data.data)
            })
          }
        } else {
          app.tips(msg);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let page = this.data.current_page + 1;
    this.getData({page})
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})